#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<errno.h>



int main(int argc, char* argv[], char *env[]){
  (void)argc;
  (void)argv;
  (void)env; 
    
  pid_t id = fork();
  if(id == 0) {
    int cnt = 5;
    while(cnt){
      printf("我是一台子进程， pid：%d, ppid: %d\n", getpid(), getppid());
      sleep(1);
      cnt--;
    }
    exit(1);
  }
  sleep(10);
  //pid_t rid = wait(NULL);
  int status = 0;
  pid_t rid = waitpid(-1, &status, 0);
  if(rid > 0){
    printf("wait success, rid: %d, status: %d, exit code: %d, exit signal: %d\n", rid, status, (status) >> 8 & 0xFF, (status) & 0x7F);
  }
  else{
    printf("wait failed: %d: %s\n", errno, strerror(errno));
  }
  sleep(10);
  return 0;
}
